<?php
//https://docs.adapay.tech/api/index.html
namespace jy_adapay;
defined('BY_JYA') or exit('error');
require_once ROOT_PLUGIN.'jy_adapay/api/AdapaySdk/init.php';
class pay extends \plugin{
	public function __construct($setting=array()){
		parent::__construct('jy_adapay');
		$setting=uni_setting('pay');
		if(!$setting['adapay_appid'] || !$setting['adapay_key_live'] || !$setting['adapay_private_key']  || !$setting['adapay_public_key']){
			exi('应用app_id，prod模式APIKEY，商户RSA私钥，Adapay RSA公钥都必须填写','error');
		}
		$this->app_id=$setting['adapay_appid'];
		$this->token_no=$setting['adapay_token_no'];
		$config_object = [
			"api_key_live" => $setting['adapay_key_live'],
			"api_key_test" => $setting['adapay_key_test'],
			"rsa_private_key" => $setting['adapay_private_key'],
			"rsa_public_key" => $setting['adapay_public_key']
		];
		\AdaPay\AdaPay::init($config_object, "live", true);
	}
	//创建订单
	public function create($pay_log,$paytype=''){	
		$payment = new \AdaPaySdk\Payment();
		# 支付设置
		if($paytype=='wechat' && $_SESSION['container']=='wechat'){
			$paytype='wx_pub';
		}
		if($paytype=='alipay'){
			$paytype='alipay_wap';
		}
		$device_info=array('device_type'=>1,'device_ip'=>CLIENT_IP);
		if($paytype=='wx_pub'){
			$expend=array(
				'open_id'=>$pay_log['openid']?:$_SESSION['openid'],
			);
		}elseif($paytype=='alipay_wap'){
			$expend=array(
				'limit_pay'=>1,
			);
		}elseif($paytype=='union_wap'){
			$user_identity_id=$this->user_identity_id();
			$expend=array(
				'user_identity_id'=>$user_identity_id,
				'client_ip'=>CLIENT_IP,
				'callback_url'=>$pay_log['back_url'],
			);
		}elseif($paytype=='union_online'){
			$expend=array(
				'client_ip'=>CLIENT_IP,
				'callback_url'=>$pay_log['back_url'],
			);
		}elseif($paytype=='fast_pay'){
			$expend=array(
				'token_no'=>$this->token_no,
			);
		}
		$payment_params = array(
			'app_id'=> $this->app_id,
			'order_no'=> $pay_log['tid']?:$pay_log['ordersn'],
			'pay_channel'=>$paytype,
			'pay_amt'=>$pay_log['fee'],
			'goods_title'=>$pay_log['title'],
			'goods_desc'=>$pay_log['title'],
			'device_info'=>$device_info,
			'expend'=>$expend,
			'notify_url'=>$pay_log['notify_url']?:SITEROOT .'addons/jy_adapay/notify.php',
		);
		//print_r($payment_params);exit;
		# 发起支付
		$payment->create($payment_params);
		# 对支付结果进行处理
		if ($payment->isError()){
			$res=$payment->result;
			if($res['error_code']){
				exi('支付失败:'.$res['error_msg'].',错误码：'.$res['error_code'],'error');
			}
		} else {
			$res=$payment->result;
		}
		$expend=$res['expend'];
		if($paytype=='alipay_wap'){
			return $expend['pay_info'];
		}elseif($paytype=='wx_pub'){
			$package=array(
				'appId'=>$res['app_id'],
				'timeStamp'=>"".TIMESTAMP,
				'package'=>'prepay_id='.$res['id'],
				'signType'=>'MD5',
				'nonceStr'=>random(8),
			);
			$package['paySign']=cfc('wechat_pay')->md5_sign($package);
			return $package;
		}elseif($paytype=='union_wap'){
			return $expend['pay_url'];
		}elseif($paytype=='union_online'){
			return $expend['form_html'];
		}elseif($paytype=='fast_pay'){
			return $expend['trans_assembled_url'];
		}
		return $res;
	}
	//验证签名
	public function check_sign($post){
		$adapay_tools = new \AdaPaySdk\AdapayTools();
		$post_data = json_decode($post['data'],1);
		$post_data_str = json_encode($post_data,JSON_UNESCAPED_UNICODE);
		$post_sign_str = isset($post['sign']) ? $post['sign']: '';
		$sign_flag = $adapay_tools->verifySign($post_data_str, $post_sign_str);
		if ($sign_flag){
			return true;
		}else{
			return false;
		}
	}
	public function user_identity_id($post){
		# 获取银联云闪付用户标识
		$union_params = array(
			# app_id
			'app_id'=> 'app_7d87c043-aae3-4357-9b2c-269349a980d6',
			# 用户授权码
			'user_auth_code'=> '5yRGbi+IRda5khIQoQf1Hw==',
			# App 标识
			'app_up_identifier'=> 'CloudPay',
			# 订单号
			'order_no'=> "_". date("YmdHis").rand(100000, 999999)
		);
		# 获取银联云闪付用户标识
		$union_user->unionUserId($union_params);
		# 获取银联云闪付用户标识结果进行处理
		if ($union_user->isError()){
			$res=$union_user->result;
		} else {
			$res=$union_user->result;
		}
		return $res['user_identity_id'];
	}
}